package com.yangfan.thread.thread_wangwenjun14;

/**
 * 自己实现 CountDown 功能
 */
public class CountDown {

    //这个值是用来 记录 一共 要同步几个线程的
    private final int total;

    //用来记录完成任务的线程数量
    private int counter = 0;

    public CountDown(int total) {
        this.total = total;
    }

    /**
     * 当线程执行完毕后调用
     */
    public void down() {
        synchronized (this) {
            this.counter++;
            this.notifyAll();
        }
    }

    public void await() throws InterruptedException {
        synchronized (this) {
            while (counter != total) {
                this.wait();
            }
        }
    }
}